package com.ling.module1sbbyyds.service.community;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ling.sbbyyds.interaction.param.CommunityTalkTypePo;
import com.ling.sbbyyds.entity.community.CommunityTalkType;
import com.ling.sbbyyds.imagetools.ImageUtils;
import com.ling.sbbyyds.interaction.ResultVo;
import com.ling.sbbyyds.mapper.community.CommunityTalkTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * 天道酬勤、学无止境
 *
 * @Description: 社区话题分类
 * @author: ling
 * @date: 2022年03月27日
 */
@Service
public class CommunityTalkTypeService {

    @Autowired(required = false)
    private CommunityTalkTypeMapper communityTalkTypeMapper;

    /**
     * 获取所有话题类型 列表
     *
     * @param page                页数
     * @param limit               条数
     * @param communityTalkTypePo 条件参数
     * @return
     */
    public ResultVo getCommunityTalkTypeList(Integer page, Integer limit, CommunityTalkTypePo communityTalkTypePo) {
        Example example = new Example(CommunityTalkType.class);
        //新建一个 筛选条件对象
        Example.Criteria criteria = example.createCriteria();
        if (!ObjectUtils.isEmpty(communityTalkTypePo.getCommunityTalkTypeId())) {
            //字段不为空，设置条件查询
            criteria.andEqualTo("communityTalkTypeId", communityTalkTypePo.getCommunityTalkTypeId());
        }
        if (!ObjectUtils.isEmpty(communityTalkTypePo.getCommunityTalkTypeName())) {
            criteria.andEqualTo("communityTalkTypeName", communityTalkTypePo.getCommunityTalkTypeName());
        }
        //排序
        example.setOrderByClause("sort asc");
        //进行分页查找
        List<CommunityTalkType> communityTalks;
        if (!ObjectUtils.isEmpty(page) && !ObjectUtils.isEmpty(limit)) {
            final PageInfo<CommunityTalkType> pageInfo = PageHelper.startPage(page, limit)
                    .doSelectPageInfo(() -> this.communityTalkTypeMapper.selectByExample(example));
            communityTalks = pageInfo.getList();
        } else {//不分页
            communityTalks = communityTalkTypeMapper.selectByExample(example);
        }

        for (CommunityTalkType communityTalk : communityTalks) {
            communityTalk.setBackground(ImageUtils.convertImageUrl(communityTalk.getBackground()));
        }

        return ResultVo.success(communityTalks);
    }


}
